﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace remes.BLoc.UI
{
   /// <summary>
   /// Interaction logic for BLocProjectDialog.xaml
   /// </summary>
   public partial class BLocProjectDialog : Window
   {
      public BLocProjectDialog()
      {
         Project = null;

         InitializeComponent();
      }

      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
         if (Project != null)
         {
            PreBuildActionNoneRadioButton.IsChecked = Project.PreBuildActions == 0;
            PreBuildActionCheckUIDsRadioButton.IsChecked = (Project.PreBuildActions & PreBuildActions.CheckUID) != 0;
            PreBuildActionUpdateUIDsRadioButton.IsChecked = (Project.PreBuildActions & PreBuildActions.UpdateUID) != 0;

            PostBuildActionParseAssemblyCheckBox.IsChecked = (Project.PostBuildActions & PostBuildActions.ParseAssembly) != 0;
            PostBuildActionGenerateAssembliesCheckBox.IsChecked = (Project.PostBuildActions & PostBuildActions.GenerateAssemblies) != 0;
         }
      }

      public BLocProject Project { get; set; }

      private void ApplySettingsButton_Click(object sender, RoutedEventArgs e)
      {
         if (Project != null)
         {
            try
            {
               Project.UpdateProjectFile();
            }
            catch (Exception ex)
            {
               MessageBox.Show(this, ex.Message, "Updating VS Project file", MessageBoxButton.OK, MessageBoxImage.Error);
            }
         }
      }

      private void SelectOriginalLangButton_Click(object sender, RoutedEventArgs e)
      {
         if (Project != null)
         {
            var dlg = new SelectCultureInfoDialog();
            dlg.Owner = this;
            var result = dlg.ShowDialog();
            if (result.HasValue && result.Value && dlg.SelectedLanguage != null)
            {
               Project.OriginalLang = dlg.SelectedLanguage;
            }
         }
      }

      private void PreBuildActionRadioButton_Checked(object sender, RoutedEventArgs e)
      {
         if (Project != null)
         {
            Project.PreBuildActions = 0;
            if (PreBuildActionCheckUIDsRadioButton.IsChecked.Value)
            {
               Project.PreBuildActions |= PreBuildActions.CheckUID;
            }
            if (PreBuildActionUpdateUIDsRadioButton.IsChecked.Value)
            {
               Project.PreBuildActions |= PreBuildActions.UpdateUID;
            }
         }
      }

      private void PostBuildActionCheckBox_Changed(object sender, RoutedEventArgs e)
      {
         if (Project != null)
         {
            Project.PostBuildActions = 0;
            if (PostBuildActionParseAssemblyCheckBox.IsChecked.Value)
            {
               Project.PostBuildActions |= PostBuildActions.ParseAssembly;
            }
            if (PostBuildActionGenerateAssembliesCheckBox.IsChecked.Value)
            {
               Project.PostBuildActions |= PostBuildActions.GenerateAssemblies;
            }
         }
      }
   }
}
